/*******************************************************************************
This file manipulates the sectoral PPI and cost shock data, to save it
in a format readable by the Jackson et al. / Otrok GAUSS scripts
(that is a mix of wide/long format)
*******************************************************************************/
clear
set more off

*global rootfolder ""
global folder "$rootfolder\analysis"
global inputfolder17X31 "$folder\COSTSHOCKS\17X31"
global inputfolder18X31 "$folder\COSTSHOCKS\18X31"
global inputfolder35X31 "$folder\COSTSHOCKS\35X31"
global CSfolder "$folder\CS"
global PPIfolder "$folder\PPI"
global WIODfolder "$folder\WIOD"

global otrokfolder "$rootfolder\analysis\factorAnalysis\Otrok_threeFactors"

capture mkdir "$otrokfolder"
capture mkdir "$otrokfolder\data"
capture mkdir "$otrokfolder\Otrok_output"
capture mkdir "$otrokfolder\Otrok_output\COST_baseline"
capture mkdir "$otrokfolder\Otrok_output\COST_baseline\alldraws"
capture mkdir "$otrokfolder\Otrok_output\PPI_baseline"
capture mkdir "$otrokfolder\Otrok_output\PPI_baseline\alldraws"

cd "$rootfolder\analysis\FactorAnalysis"

******************************************************************************************
* Import PPI and cost shocks from excel (prepared in matlab)
******************************************************************************************

*******************
* 17 X 31
*******************
import excel using "$inputfolder17X31\\costShocks_allPeriods_table.xlsx",clear firstrow
save "$inputfolder17X31\\costShocks_allPeriods_table.dta", replace

*******************
* 18 X 31
*******************
import excel using "$inputfolder18X31\\costShocks_allPeriods_table_18X31.xlsx",clear firstrow
save "$inputfolder18X31\\costShocks_allPeriods_table_18X31.dta", replace

*******************
* 35 X 31
*******************
import excel using "$inputfolder35X31\\costShocks_allPeriods_table_higherOrder.xlsx",clear firstrow
save "$inputfolder35X31\\costShocks_allPeriods_table_higherOrder.dta", replace

**************************************
* Merge 
**************************************
use "$inputfolder17X31\\costShocks_allPeriods_table.dta", clear
merge 1:1 country country_ind year month using "$inputfolder18X31\\costShocks_allPeriods_table_18X31.dta"
count if _merge != 3 
assert r(N) == 6324 // services sector
drop _merge
merge 1:1 country country_ind year month using "$inputfolder35X31\\costShocks_allPeriods_table_higherOrder.dta"
count if _merge != 3 
assert r(N) == 6324 // services sector
drop _merge
drop PPIoo

* Adjustments
drop date
gen date = ym(year,month)
format date %tm
egen id = group(country country_ind)
xtset id date

* Rename
rename PPI PPI_baseline
rename PPI_balI PPI_bal1
rename PPI_balII PPI_bal2
rename PPI_sagg PPI_sagg_s
rename PPI_scpi PPI_scpi_s
rename COST_mechpt_0333 COST_mechpt_1over3 
rename COST_mechpt_0667 COST_mechpt_2over3
rename COST_pcomp_0333  COST_pcomp_1over3 
rename COST_pcomp_0667  COST_pcomp_2over3
rename COST_sagg  COST_sagg_s
rename COST_scpi  COST_scpi_s

ds, has(type double)
global ppicostlist `r(varlist)'
macro list ppicostlist

* Compute 12-month growth rates
sort id date
foreach var of varlist $ppicostlist {
	gen AA`var' = (1 + l11.`var')*(1+l10.`var')*(1+l9.`var')*(1+l8.`var')*(1+l7.`var')* ///
		(1+l6.`var')*(1+l5.`var')*(1+l4.`var')*(1+l3.`var')*(1+l2.`var')*(1+l1.`var')*(1+`var') - 1

	drop `var'
	rename AA`var' `var'
}

summarize year
drop if ym(year,month) < ym(`r(min)',12) // drop first 11 months
drop year month

* Reshape by industry
drop id
sort country country_ind date
drop if country_ind == "Srest" // baseline

reshape wide $ppicostlist, i(date country) j(country_ind) string

sort country date
outsheet PPI_baseline* using "$otrokfolder\data\gvc_data_PPI_baseline_series.txt", non replace
outsheet COST_baseline* using "$otrokfolder\data\gvc_data_COST_baseline_series.txt", non replace

* Export country names to excel for Gauss output
keep if date == ym(1995,12)
drop if country == "ROW"
export excel country using "Tables.xlsx", cell(A2) sheet("Table_A4", modify) firstrow(variables) 
export excel country using "Tables.xlsx", cell(F2) sheet("Table_A4", modify) firstrow(variables) 

*Prepare weights for each series, 2002-06
use "$WIODfolder\reduced_WIOD.dta", clear
collapse (firstnm) total_output, by(year c_I s_I country country_ind)
sort year country country_ind
expand 12		
bysort year c_I s_I: gen month = _n
order year month
sort year month country country_ind
merge m:1 year month country country_ind using "$PPIfolder\NM_Price_changes_monthly_trunc.dta"
drop if _merge == 2
drop _merge

bys country country_ind: egen mean_mi = mean(_extr_chng_ppi)
gen missingall = 0
replace missingall = 1 if mean_mi == 1

keep if year==2002 & month==6

gen output_for_weight = total_output*(1-missingall)
bys country: egen totoutput_for_weight = total(output_for_weight)
gen weight = output_for_weight/totoutput_for_weight if missingall==0
replace weight=0 if missingall==1 // series fully imputed have constant 12month change, so we attribute them a weight of 0
sort country country_ind

outsheet weight using "$otrokfolder\data\weights_full_fullimpute0.txt", non replace

* Next
cd "$rootfolder\analysis"
